package com.egg.ylt.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.activity.ACT_NewsDetails;
import com.egg.ylt.activity.ACT_VideoPlaying;
import com.egg.ylt.adapter.ADA_ConsultRecyclerView;
import com.egg.ylt.pojo.HomePageEntity;
import com.egg.ylt.presenter.impl.NewsPresenterImpl;
import com.egg.ylt.view.INewsView;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.yonyou.framework.library.base.BaseFragment;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.ListUtil;
import com.yonyou.framework.library.eventbus.EventCenter;

import butterknife.BindView;

public class FRA_NewsItem extends BaseFragment<NewsPresenterImpl> implements INewsView {
    @BindView(R.id.fra_news_recycler_view)
    XRecyclerView fraNewsRecyclerView;
    @BindView(R.id.target_view_ll)
    LinearLayout llTargetView;

    private ADA_ConsultRecyclerView mNewsAdapter;

    private String pageSize = "5";
    private int listSize = 0;
    private int pageNum = 1;
    /**
     * 资讯类型：0-婴儿游泳，1-产后康复，2-儿童推拿
     */
    private String messageClassify = "0";
    //下拉刷新 和 上拉加载状态值
    private int mRefreshStateInt = 0;


    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {
//        if (mPresenter != null){
//            //请求门店展示数据
//            mRefreshStateInt = 0;
//            pageNum = 1;
//            fraNewsRecyclerView.setNoMore(false);
//            mPresenter.getNewsData(Constants.COMPANYID, String.valueOf(pageNum), pageSize, messageClassify);
//        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (mPresenter != null && mRefreshStateInt == 0){
            //请求门店展示数据
            mRefreshStateInt = 0;
            pageNum = 1;
            fraNewsRecyclerView.setNoMore(false);
            mPresenter.getNewsData(Constants.COMPANYID, String.valueOf(pageNum), pageSize, messageClassify);
        }
    }

    @Override
    protected void onUserInvisible() {

    }

    public void refreshNews(){
        if (mPresenter != null){
            //请求门店展示数据
            mRefreshStateInt = 0;
            pageNum = 1;
            fraNewsRecyclerView.setNoMore(false);
            mPresenter.getNewsData(Constants.COMPANYID, String.valueOf(pageNum), pageSize, messageClassify);
        }
    }

    public void setRefreshOnNextShow(){
        mRefreshStateInt = 0;
    }

    @Override
    protected View getLoadingTargetView() {
        return llTargetView;
    }

    @Override
    protected void initViewsAndEvents() {
        //根据
        //初始化当前fragment
        int position = getArguments().getInt(FRA_NewsItem.class.getName(), -1);
        switch (position) {
            case 0:
                messageClassify = "0";
                break;
            case 1:
                messageClassify = "1";
                break;
            case 2:
                messageClassify = "2";
                break;
        }

        //recyclerview
        initRecycler();
    }

    /**
     * 设置listview
     */
    private void initRecycler() {
        //设置布局管理器
        fraNewsRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        //设置适配器
        mNewsAdapter = new ADA_ConsultRecyclerView(mContext);
        fraNewsRecyclerView.setAdapter(mNewsAdapter);
        fraNewsRecyclerView.setHasFixedSize(true);
        fraNewsRecyclerView.setNestedScrollingEnabled(false);
        fraNewsRecyclerView.setFocusable(false);

        //list的条目监听
        mNewsAdapter.setOnItemClickListener(new ADA_ConsultRecyclerView.OnItemClickListener() {
            @Override
            public void onItemClick(String id, String type) {
                Bundle mBundle = new Bundle();
                mBundle.putString("id", id);
                switch (type) {
                    case "0":
                    case "1":
                    case "2":
                        readyGo(ACT_NewsDetails.class, mBundle);
                        break;
                    case "3":
                        readyGo(ACT_VideoPlaying.class, mBundle);
                        break;
                }
            }
        });


        initRefresh();
    }

    private void initRefresh() {

        fraNewsRecyclerView.setPullRefreshEnabled(false);
        fraNewsRecyclerView.setLoadingMoreEnabled(true);
        fraNewsRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
        fraNewsRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                    mRefreshStateInt = 1;
                    pageNum++;
                    //资讯信息
                    mPresenter.getNewsData(Constants.COMPANYID, String.valueOf(pageNum), pageSize, messageClassify);
            }
        });

        mPresenter.getNewsData(Constants.COMPANYID, String.valueOf(pageNum), pageSize, messageClassify);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fra_news_item;
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    public static FRA_NewsItem newInstance(int status) {
        FRA_NewsItem fragment = new FRA_NewsItem();
        Bundle bdl = new Bundle(status);
        bdl.putInt(FRA_NewsItem.class.getName(), status);
        fragment.setArguments(bdl);
        return fragment;
    }

    @Override
    public void getHomePage(HomePageEntity entity) {
        //得到资讯数据
        if (ListUtil.isListEmpty(entity.getList()) && mNewsAdapter.getItemCount() == 0) {
            toggleShowEmpty(true, R.drawable.empty_no_response, "暂无咨询信息");
            return;
        }

        toggleShowEmpty(false, -1, "");
        listSize = entity.getList().size();

        switch (mRefreshStateInt) {
            case 0:
                mNewsAdapter.addList(entity.getList());
                break;
            case 1:
                mNewsAdapter.updateList(entity.getList());
                fraNewsRecyclerView.loadMoreComplete();
                break;
        }

        if (pageNum > 1 && ListUtil.isListEmpty(entity.getList())) {
            fraNewsRecyclerView.setNoMore(true);
        }

    }
}
